// Learn cc.Class:
//  - [Chinese] http://www.cocos.com/docs/creator/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/class/index.html
// Learn Attribute:
//  - [Chinese] http://www.cocos.com/docs/creator/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/reference/attributes/index.html
// Learn life-cycle callbacks:
//  - [Chinese] http://www.cocos.com/docs/creator/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/life-cycle-callbacks/index.html
var users = require("zjhpanelView");
var log =require("Log");
//widget添加响应事件
var addButtonEventS =function (btn,i,efunc) {

    btn.on(cc.Node.EventType.TOUCH_END, function(event){
        if (efunc) { efunc(i,btn); }
    });

};

cc.Class({
    extends: users,
    properties: {
        className:{
            default:"myPanelView",
            override:true,
        },
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},
    onLoad: function(){
        this._super();
        EventManager.addEventObeserveF(EventConfig.kaipai, handler(this,
            this.kaipai),"myPanelView");
        this._isKaiPai = false ;
        this.kanpai = cc.find("kanpai",this.node);
        this.kanpai.active = false ;
    },
    //发牌
    sendCard: function(info,noAni){
        var self = this ;
        this.bgpanel.active = true;
        var pos = this.bgpanel.convertToWorldSpaceAR(this.bgpanel.getPosition())
        var tx = pos.x;
        var ty = pos.y;
        var card_tmp1  = cc.instantiate(this.Card);
        var size      = card_tmp1.getContentSize();
        var width = size.width*0.7 ;
        var alllength = width*5;

        var panelsize   = this.bgpanel.getContentSize();
        var margin = 0;
        var startX = (panelsize.width - 98*0.7 - 50*2 -50)/2 +10 ;
        console.log("发牌的大小");
        dump(size)
        this.bgpanel.removeAllChildren();
        this._cards={};
        for( i=1;i<4;i++){
            global.Audio.playSFX("games/douniu/sound/effect/card_send_one.wav");
            var card_tmp  = cc.instantiate(this.Card);       
            this._cards[i]=card_tmp;
            this.bgpanel.addChild(card_tmp,0);
            card_tmp.thisJS.setValue(0);
            if(noAni){
                card_tmp.scale = 0.7;
                card_tmp.setPosition(cc.p(startX+size.width*0.7/2+(50)*(i-1),size.height*0.7/2));
                card_tmp.thisJS.showFront();
            }
            else{
                card_tmp.scale = 0.7;
                card_tmp.setPosition(720/2-tx,1280/2-ty);

                var delay     = cc.delayTime(this.Card_Anim_Space*(5-i))
                log.logd(size.width/2,"ww-");
                var move      = cc.moveTo(this.Card_Anim_Time,cc.p(size.width*0.7/2 + startX,size.height*0.7/2))
                var scale     = cc.scaleTo(this.Card_Anim_Time,0.7)
                var spawn     = cc.spawn(move,scale)
                var delay_1ms = cc.delayTime(this.Card_Anim_Time)
                var move_line = cc.moveTo(this.Card_Anim_Time,cc.p(startX+size.width*0.7/2+(50)*(i-1),size.height*0.7/2))
                var delay_2ms = cc.delayTime(this.Card_Anim_Time*i*0.5)
                card_tmp.value = 0 ;
                card_tmp.index = i ;
                var call  = cc.callFunc(function (card_tmp){
                    // card_tmp.thisJS.setValue(card_tmp.value);
                    // if (card_tmp.index<=3){
                    // card_tmp.thisJS.showFront();
                    // }
                },this,card_tmp)
                var sq        = cc.sequence(delay,spawn,delay_1ms,move_line,delay_2ms,call);

                card_tmp.runAction(sq);
            }
        }
    },
    clearTable:function(){
        this._super();
        this.kanpai.active = false ;
    },    
    showKanCard:function(info){
        this.showEndCard(info);
    },
    showLoseCard:function(){
        return ;
    },
    updateKanState:function(state)
    {
      this.kanpai.active  = state ;  
    },
    //用户显示扑克
    showEndCard:function(info,isQi){
        var index = 1 ;
        var self = this ;
        var tempWidth = 0 ;
        var card_tmp1 = this._cards[index] ;
        if(card_tmp1 == null){
            console.log("牌被清掉了");
            return ;
        }
        var size = card_tmp1.getContentSize();
        var width = size.width*0.7 ;
        var alllength = 2*50+width;
        var panelsize   = this.bgpanel.getContentSize();
        var startWidth = (panelsize.width -alllength)/2 +30;
        console.log(startWidth+"2112");
        console.log("info-Card");
        dump(info.card)
        console.log("cardData");
        for(var k in info.cards){
            if(this._cards[index]){
                console.log("重设牌值"+info.cards[k]);
                this._cards[index].thisJS.setValue(info.cards[k]);
                this._cards[index].thisJS.showFront();
                if (isQi){
                   // this.optstate.active = true ;
                   // this.optstate.getComponent(cc.Sprite).spriteFrame = this.qi_frame ;
                    this._cards[index].thisJS.dark();
                }
                var card_tmp = this._cards[index] ;
                var size = card_tmp.getContentSize();
                card_tmp.setPosition(cc.p((index-1)*50+startWidth,size.height*0.7/2));
            }
            index = index + 1;
        }
        // var fenImg = this.fenImg.getComponent(cc.Sprite);
        // var cardTypeFile = {
        //     0:"niu_0",1:"niu_1",2:"niu_2",3:"niu_3",
        //     4:"niu_4",5:"niu_5",6:"niu_6",7:"niu_7",
        //     8:"niu_8",9:"niu_9",10:"niu_10",15:"niu_11",
        //     17:"dn_wuhua",18:"dn_wuxiao"
        // }

        // console.log("---------------");
        // console.log(cardTypeFile[info.card_type]);
        // fenImg.spriteFrame = this.score_altas.getSpriteFrame([cardTypeFile[info.card_type]]);
        // self.fenImg.active = true ;


        // if(info.win_money)
        // {    
        //     console.log("飘钱"+info.win_money);
        //     this.winMoneyFly(info.win_money);
        // }
        // console.log("飘钱mei");
    },
    //通知玩家开牌
    kaipai:function(){
        console.log("kaipai");
        var cards = GameData.playerinfo[UserInfoModel.info.uin]["cards"];
        dump(cards);
        var index = 1 ;
        var self = this ;
        var tempWidth = 0 ;
        var card_tmp1 = this._cards[index] ;
        var size = card_tmp1.getContentSize();
        var width = size.width*0.7 ;
        var alllength = 4*50+20+width;
        console.log(startWidth+"2112");
        var panelsize   = this.bgpanel.getContentSize();
        var startWidth = (panelsize.width -alllength)/2;
        console.log(startWidth+"2112");
        for(var k in cards){
            this._cards[index].thisJS.setValue(cards[k]);
            if(this._cards[index] && index<5){        
                this._cards[index].thisJS.showFront();
            }
            index = index + 1;
        }
        this._isKaiPai = true ;
        global.Audio.playSFX("games/douniu/sound/effect/card_open_one.wav");
    },
    // //显示牌型
    // showCardType:function(card_type){
    //     this.niu_bg.active = true;
    //     var fenImg = this.fenImg.getComponent(cc.Sprite);
    //     var cardTypeFile = {
    //         0:"dn_meiniu",1:"dn_niuyi",2:"dn_niuer",3:"dn_niusan",
    //         4:"dn_niusi",5:"dn_niuwu",6:"dn_niuliu",7:"dn_niuqi",
    //         8:"dn_niuba",9:"dn_niujiu",10:"dn_niuniu",15:"dn_sizha",
    //         17:"dn_wuhua",18:"dn_wuxiao"
    //     }
    //     cc.loader.loadRes("cn/game_niuniu/ui/game_douniu/game_douniu", cc.SpriteAtlas, function (err, atlas) {
    //         var frame = atlas.getSpriteFrame(cardTypeFile[card_type]);
    //         fenImg.spriteFrame = frame;
    //     });
    //     if(card_type==0){
    //         for(var k in this._cards){
    //             this._cards[k].thisJS.dark();
    //         }
    //     }
    // },
    //显示最后一张牌
    // showLastCard:function(info){
    //     this._cards[5].thisJS.setValue(info.playerinfo[UserInfoModel.info.uin].cards[4]);
    //     this._cards[5].thisJS.showFront();
    //     if(info.playerinfo[UserInfoModel.info.uin].card_type==0){
    //         this.calc_panel.active = true;
    //         this.calcClass = new calcNiu();
    //         this.calcClass.initctor({calc:this.calc_panel,cards:this._cards,wuniu:this.wuniu,youniu:this.youniu});
    //     }
    //     else{
    //         if(info.playerinfo[UserInfoModel.info.uin].card_type==15){
    //             this.sizha.active = true;
    //         }
    //         else if(info.playerinfo[UserInfoModel.info.uin].card_type==17){
    //             this.wuhua.active = true;
    //         }
    //         else if(info.playerinfo[UserInfoModel.info.uin].card_type==18){
    //             this.wuxiao.active = true;
    //         }
    //         this.calcClass = null;
    //     }
    // },

    // //用户显示扑克
    // showEndCard:function(info){
    //    //this.bgpanel.active = false;
    //     var pos = this.bgpanel.convertToWorldSpaceAR(this.bgpanel.getPosition())
    //     var tx = pos.x;
    //     var ty = pos.y;
    //     var card_tmp  = cc.instantiate(this.Card);
    //     var size      = card_tmp.getContentSize();
    //     for( i=1;i<6;i++){
    //         var card_tmp  = cc.instantiate(this.Card);
    //         this.bgpanel.addChild(card_tmp,0);
    //         card_tmp.scale = 0.7;
    //         if (info.card_type == 0  ||  info.card_type > 10){
    //             card_tmp.setPosition(cc.p((i-1)*50+size.width*0.7/2,0))
    //         }
    //         else if (info.card_type >=1 && info.card_type<=10){
    //             if (i <=3){
    //                 card_tmp.setPosition(cc.p((i-1)*50+size.width*0.7/2,0))
    //             }
    //             else{
    //                 card_tmp.setPosition(cc.p((i-1)*50+size.width*0.7/2+30,0))
    //             }
    //         }
    //         if (info.card[i-1]){
    //             card_tmp.thisJS.setValue(info.card[i-1]);
    //             card_tmp.thisJS.showFront();
    //         }
    //         if (info.card_type==0){
    //             card_tmp.thisJS.dark();
    //         }
    //     }


    //     for(var k in info.cards){
    //         if(this._cards[k]){
    //             this._cards[k].setValue(info.cards[k]);
    //             this._cards[k].thisJS.showFront();
    //         }
    //     }
    //     //this.showCardType(info.card_type);
    // },
    // //断线重连
    // reconnect:function(info){
    //     if(this._info.status>=1081){
    //         this.sendCard(this._info.cards,true);
    //         if(info.zhuang_uin==this._info.uin){
    //             this.updateDealer();
    //         }
    //         if(this._info.status==1081){
    //             this.showAskZhuangP(info);
    //         }
    //         else if(this._info.status==1082){
    //             this.showAddBetP(info);
    //         }
    //         else if(this._info.status==1083){
    //             if(info.zhuang_uin!=this._info.uin){
    //                 this.showAddBetP(info);
    //             }
    //         }
    //         else if(this._info.status==1084){
    //             if(info.zhuang_uin!=this._info.uin){
    //                 this.updateCallDealer(this._info);
    //             }
                
    //         }
    //         else if(this._info.status==1085){
    //             this.showLastCard(info);
    //         }
    //         else if(this._info.status==1086){
    //             this.showCardType(this._info.card_type);
    //         }
    //     }
    // },
    // update (dt) {},
});
